<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket</title>
</head>
<body>
	<input type="submit" value="发送数据" onclick="song()">
	<input type="text" id="text">
	<div id="msg"></div>
</body>
<script>
    
    var ws = new WebSocket("ws://localhost:4000");
ws.onopen = function(){
    console.log("握手成功");
};
ws.onmessage = function(e){
    console.log("message:" + e.data);
};
ws.onerror = function(){
    console.log("error");
};
ws.send("李靖");   
    
//	var msg = document.getElementById("msg");
//	var wsServer = 'ws://localhost:4000';
//	//调用websocket对象建立连接：
//	//参数：ws/wss(加密)：//ip:port （字符串）
//	var websocket = new WebSocket(wsServer);
//	//onopen监听连接打开
//	websocket.onopen = function(evt) {
//		//websocket.readyState 属性：
//		/*
//		CONNECTING    0    The connection is not yet open.
//		OPEN    1    The connection is open and ready to communicate.
//		CLOSING    2    The connection is in the process of closing.
//		CLOSED    3    The connection is closed or couldn't be opened.
//		 */
//		msg.innerHTML = websocket.readyState+'<br>';
//	};
//
//	//监听连接关闭
//	websocket.onclose = function(evt) {
//		console.log("Disconnected");
//	};
//
//	//监听连接错误信息
//	websocket.onerror = function(evt, e) {
//		console.log('Error occured: ' + evt.data);
//	};
//
//	//onmessage 监听服务器数据推送
//	websocket.onmessage = function(evt) {
//		msg.innerHTML += evt.data + '<br>';
//		console.log('Retrieved data from server: ' + evt.data);
//	};
//
//	function song() {
//		var text = document.getElementById('text').value;
//		document.getElementById('text').value = '';
//		//向服务器发送数据
//		var data = '{"type":"customer","message":"'+text+'"}';
//		websocket.send(data);
//	}
</script>
</html>
